package com.ruoyi.hikvision.hk;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.tool.Md5Utils;
import com.ruoyi.hikvision.domain.HikvisionDevice;
import com.ruoyi.hikvision.service.IHikvisionDeviceService;
import com.ruoyi.wisdom.constants.IotConstants;
import com.ruoyi.wisdom.domain.IotPickFace;
import com.ruoyi.wisdom.domain.IotPickRecord;
import com.ruoyi.wisdom.domain.activity.DetectSource;
import com.ruoyi.wisdom.helper.HkPick;
import com.ruoyi.wisdom.service.IIotPickFaceService;
import com.ruoyi.wisdom.service.IIotPickRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class HikvisionDataLogic {

    @Autowired
    private HkPick hkPick;

    @Autowired
    private IHikvisionDeviceService hikvisionDeviceService;

    @Autowired
    private IIotPickFaceService iotPickFaceService;

    @Autowired
    private IIotPickRecordService iotPickRecordServic;

    private final List<String> hashList = new ArrayList<>();
    private final Map<String, HikvisionDevice> hikvisionDeviceMap = new HashMap<>();
    private final Map<String, IotPickFace> pickFaceMap = new HashMap<>();

    private HikvisionDevice getPickDevice(String deviceNo) {
        if (StringUtils.isEmpty(deviceNo)) return null;
        HikvisionDevice hikvisionDevice = hikvisionDeviceMap.getOrDefault(deviceNo, null);
        if (hikvisionDevice == null) {
            hikvisionDevice = hikvisionDeviceService.selectHikvisionDeviceByDeviceNo(deviceNo);
            if (hikvisionDevice != null) hikvisionDeviceMap.put(deviceNo, hikvisionDevice);
        }
        return hikvisionDevice;
    }

    private IotPickFace getPickFace(String employeeNo) {
        if (StringUtils.isEmpty(employeeNo)) return null;
        IotPickFace pickFace = pickFaceMap.getOrDefault(employeeNo, null);
        if (pickFace == null) {
            pickFace = iotPickFaceService.selectIotPickFaceByFaceCode(employeeNo);
            if (pickFace != null) pickFaceMap.put(employeeNo, pickFace);
        }
        return pickFace;
    }

    // 保存出入记录
    public void savePickRecord(String deviceNo, String employeeNo, String strTime) {
        HikvisionDevice pickDevice = getPickDevice(deviceNo);
        if (pickDevice == null) return;
        IotPickFace pickFace = getPickFace(employeeNo);
        if (pickFace == null) return;
        String hash = Md5Utils.encode32(deviceNo + employeeNo + strTime);
        if (hashList.contains(hash)) return;
        hashList.add(hash);
        IotPickRecord check = iotPickRecordServic.selectIotPickRecordByHash(hash);
        if (check != null) return;

        // 关联场景
        DetectSource detectData = DetectSource.builder()
                .deviceNo(deviceNo)
                .facecode(pickFace.getFacecode())
                .personType(pickFace.getPersonType())
                .uuid(pickFace.getUuid())
                .faceTime(strTime)
                .build();

        if (pickFace.getPersonType() == IotConstants.USER_TYPE_STUDENT) {
            hkPick.handleStudent(detectData);
        } else {
            hkPick.handleTeacher(detectData);
        }

        IotPickRecord pickRecord = new IotPickRecord();
        pickRecord.setComId(pickFace.getComId());
        pickRecord.setDeviceNo(deviceNo);
        pickRecord.setFacecode(pickFace.getFacecode());
        pickRecord.setUuid(pickFace.getUuid());
        pickRecord.setName(pickFace.getName());
        pickRecord.setPersonType(pickFace.getPersonType());
        pickRecord.setType(pickDevice.getType());
        pickRecord.setFaceTime(strTime);
        pickRecord.setLocation(pickDevice.getLocation());
        pickRecord.setRelationId(pickFace.getRelationId());
        pickRecord.setHash(hash);
        //iotPickRecordServic.insertIotPickRecord(pickRecord);
    }
}
